package club.yunqiang.controller;

import club.yunqiang.dto.ModifyDBInstanceConnection;
import club.yunqiang.dto.ModifyDBInstanceNetworkExpireTime;
import club.yunqiang.service.RdsApiService;
import club.yunqiang.service.RdsSdkService;
import com.aliyuncs.rds.model.v20140815.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author YunQiang
 */
@Api(value = "/rds", description = "RDS 网络管理")
@RestController
@RequestMapping("/rds")
public class RdsController {

	@Autowired
	private RdsSdkService rdsService;

	@Autowired
	private RdsApiService rdsApiService;

	@ApiOperation(value = "申请实例的外网连接串(SDK实现)")
	@PostMapping("/allocateInstancePublicConnection")
	public Object allocateInstancePublicConnection(@ApiParam("连接串的前缀（自定义即可）") @RequestParam String connectionStringPrefix) {
		return rdsApiService.allocateInstancePublicConnection(connectionStringPrefix);
	}

	@ApiOperation(value = "申请实例的外网连接串(手动实现)")
	@PostMapping("/allocateInstancePublicConnection2")
	public Object allocateInstancePublicConnection2(@ApiParam("连接串的前缀（自定义即可）") @RequestParam String connectionStringPrefix) {
		AllocateInstancePublicConnectionResponse response = rdsService.allocateInstancePublicConnection(connectionStringPrefix);
		return response;
	}


	@ApiOperation("查看实例的所有连接信息（SDK实现）")
	@GetMapping("/describeDBInstanceNetInfo")
	public Object describeDBInstanceNetInfo() {
		DescribeDBInstanceNetInfoResponse response = rdsService.describeDBInstanceNetInfo();
		return response;
	}

	@ApiOperation("查看实例的所有连接信息（手动实现）")
	@GetMapping("/describeDBInstanceNetInfo2")
	public Object describeDBInstanceNetInfo2() {
		Object response = rdsApiService.describeDBInstanceNetInfo();
		return response;
	}

	@ApiOperation("修改连接串和端口（SDK实现）")
	@PostMapping("/modifyDBInstanceConnectionString")
	public Object modifyDBInstanceConnectionString(ModifyDBInstanceConnection modifyDBInstanceConnection) {
		ModifyDBInstanceConnectionStringResponse response = rdsService.modifyDBInstanceConnectionString(modifyDBInstanceConnection);
		return response;
	}

	@ApiOperation("修改连接串和端口（手动实现）")
	@PostMapping("/modifyDBInstanceConnectionString2")
	public Object modifyDBInstanceConnectionString2(ModifyDBInstanceConnection modifyDBInstanceConnection) {
		Object response = rdsApiService.modifyDBInstanceConnectionString(modifyDBInstanceConnection);
		return response;
	}

	@ApiOperation("修改经典网络的过期时间（VPC网络和经典网络共存）（SDK实现）")
	@PostMapping("/modifyDBInstanceNetworkExpireTime")
	public Object modifyDBInstanceNetworkExpireTime(ModifyDBInstanceNetworkExpireTime modifyDBInstanceNetworkExpireTime) {
		ModifyDBInstanceNetworkExpireTimeResponse response = rdsService.modifyDBInstanceNetworkExpireTime(modifyDBInstanceNetworkExpireTime);
		return response;
	}

	@ApiOperation("修改经典网络的过期时间（VPC网络和经典网络共存）（手动实现）")
	@PostMapping("/modifyDBInstanceNetworkExpireTime2")
	public Object modifyDBInstanceNetworkExpireTime2(ModifyDBInstanceNetworkExpireTime modifyDBInstanceNetworkExpireTime) {
		Object response = rdsApiService.modifyDBInstanceNetworkExpireTime(modifyDBInstanceNetworkExpireTime);
		return response;
	}

	@ApiOperation("修改RDS实例网络类型（即在经典网络和VPC网络间进行切换）（SDK实现）")
	@PostMapping("/modifyDBInstanceNetworkType")
	public Object modifyDBInstanceNetworkType(@ApiParam("实例的网络类型。取值范围(Classic：表示经典网络、VPC：表示VPC网络）") @RequestParam String instanceNetworkType) {
		ModifyDBInstanceNetworkTypeResponse response = rdsService.modifyDBInstanceNetworkType(instanceNetworkType);
		return response;
	}

	@ApiOperation("修改RDS实例网络类型（即在经典网络和VPC网络间进行切换）（手动实现）")
	@PostMapping("/modifyDBInstanceNetworkType2")
	public Object modifyDBInstanceNetworkType2(@ApiParam("实例的网络类型。取值范围(Classic：表示经典网络、VPC：表示VPC网络）") @RequestParam String instanceNetworkType) {
		Object response = rdsApiService.modifyDBInstanceNetworkType(instanceNetworkType);
		return response;
	}

	@ApiOperation("释放实例的外网连接串（SDK实现）")
	@PostMapping("/releaseInstancePublicConnection")
	public Object releaseInstancePublicConnection(@ApiParam("外网连接串(\"可使用\"/rds/describeDBInstanceNetInfo\"接口获取\")") @RequestParam String currentConnectionString){
		ReleaseInstancePublicConnectionResponse response = rdsService.releaseInstancePublicConnection(currentConnectionString);
		return response;
	}

	@ApiOperation("释放实例的外网连接串（手动实现）")
	@PostMapping("/releaseInstancePublicConnection2")
	public Object releaseInstancePublicConnection2(@ApiParam("外网连接串(\"可使用\"/rds/describeDBInstanceNetInfo\"接口获取\")") @RequestParam String currentConnectionString){
		Object response = rdsApiService.releaseInstancePublicConnection(currentConnectionString);
		return response;
	}

	@ApiOperation("切换内外网（实例的连接地址会发生变化）（SDK实现）")
	@PostMapping("/switchDBInstanceNetType")
	public Object switchDBInstanceNetType(@ApiParam("连接串的前缀(\"可使用\"/rds/modifyDBInstanceConnectionString\"接口自定义\")") @RequestParam String connectionStringPrefix){
		SwitchDBInstanceNetTypeResponse response = rdsService.switchDBInstanceNetType(connectionStringPrefix);
		return response;
	}

	@ApiOperation("切换内外网（实例的连接地址会发生变化）（手动实现）")
	@PostMapping("/switchDBInstanceNetType2")
	public Object switchDBInstanceNetType2(@ApiParam("连接串的前缀(\"可使用\"/rds/modifyDBInstanceConnectionString\"接口自定义\")") @RequestParam String connectionStringPrefix){
		Object response = rdsApiService.switchDBInstanceNetType(connectionStringPrefix);
		return response;
	}


}